package com.object.utils;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executor;

@Configuration
public class AsyncUtil {

    private static Executor sysTaskExecutor;

    @PostConstruct
    public  void init() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        // 初始线程数
        executor.setMaxPoolSize(10);
        // 最大线程数
        executor.setQueueCapacity(100);
        // 等待队列大小
        executor.setThreadNamePrefix("sysTask-");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        sysTaskExecutor = executor;
    }

    /**
     * 提交一个异步任务到 sysTaskExecutor 线程池
     *
     * @param task 任务
     */
    public static void executeSysTask(Runnable task) {
        sysTaskExecutor.execute(task);
    }


}